package com.weishi.project.training.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.weishi.common.utils.SecurityUtils;
import com.weishi.common.utils.StringUtils;
import com.weishi.framework.web.domain.server.Sys;
import com.weishi.project.training.domain.SportsmanBodyShape;
import com.weishi.project.training.service.ISportsmanBodyShapeService;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.weishi.framework.aspectj.lang.annotation.Log;
import com.weishi.framework.aspectj.lang.enums.BusinessType;

import com.weishi.project.training.domain.Sportsman;
import com.weishi.project.training.service.ISportsmanService;
import com.weishi.framework.web.controller.BaseController;
import com.weishi.framework.web.domain.AjaxResult;
import com.weishi.common.utils.poi.ExcelUtil;
import com.weishi.framework.web.page.TableDataInfo;

/**
 * sportsmanController
 */
@Api(value = "运动员",description = "运动员")
@RestController
@RequestMapping("/training/sportsman")
public class SportsmanController extends BaseController {

    @Autowired
    private ISportsmanService sportsmanService;

    /**
     * 身体形态 接口
     */
    @Autowired
    private ISportsmanBodyShapeService sportsmanBodyShapeService;



    /**
     * 查询运动员列表
     */
    @PreAuthorize("@ss.hasPermi('training:sportsman:list')")
    @GetMapping("/list")
    public TableDataInfo list(Sportsman sportsman) throws Exception {
        startPage();
        List<Sportsman> list = sportsmanService.selectSportsmanList(sportsman);
        return getDataTable(list);
    }

    @GetMapping("/listSportsmanCountByType")
    public AjaxResult listSportsmanCountByType() throws Exception {
        List<Map<String, Object>> list = sportsmanService.selectSportsmanCountByType();
        Map<String, Long> ret = new HashMap<>();
        long all=0,train=0,leave=0,injury=0;
        for(Map<String, Object> map : list){
            if(map.get("status").equals("0") || map.get("status").equals("2")){
                all += (long)map.get("num");
                train += (long)map.get("num");
            }if(map.get("status").equals("3")){
                leave += (long)map.get("num");
                all += (long)map.get("num");
            }if(map.get("status").equals("4")|| map.get("status").equals("5")|| map.get("status").equals("6")){
                all += (long)map.get("num");
                injury += (long)map.get("num");
            }
        }
        ret.put("all",all);
        ret.put("leave",leave);
        ret.put("train",train);
        ret.put("injury",injury);
        return AjaxResult.success(ret);
    }

    /**
     * 导出运动员列表
     */
    @PreAuthorize("@ss.hasPermi('training:sportsman:export')")
    @Log(title = "sportsman", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Sportsman sportsman) throws Exception {
        List<Sportsman> list = sportsmanService.selectSportsmanList(sportsman);
        ExcelUtil<Sportsman> util = new ExcelUtil<Sportsman>(Sportsman.class);
        return util.exportExcel(list, "sportsman");
    }

    /**
     * 获取运动员详细信息
     */
    //@PreAuthorize("@ss.hasPermi('training:sportsman:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        Sportsman sportsman = sportsmanService.getById(id);

        // 查询用动员最新一条身体形态
        List<SportsmanBodyShape> sportsmanBodyShapeList = sportsmanBodyShapeService
                .list(Wrappers.<SportsmanBodyShape>lambdaQuery()
                        .eq(SportsmanBodyShape::getSportsmanId , sportsman.getId())
                        .orderByDesc(SportsmanBodyShape::getCtime));

        if(sportsmanBodyShapeList.size() > 0){
            sportsman.setArmSpreads(sportsmanBodyShapeList.get(0).getArmSpread()); // 臂展
            sportsman.setKneelHigh(sportsmanBodyShapeList.get(0).getKneelHigh()); // 跪高（ 划艇 ）
            sportsman.setSitHighs(sportsmanBodyShapeList.get(0).getSitHigh()); // 坐高
            sportsman.setHeights(sportsmanBodyShapeList.get(0).getHeight()); // 身高
            sportsman.setShoulderWidths(sportsmanBodyShapeList.get(0).getShoulderWidth()); // 肩宽
            sportsman.setUpperArm(sportsmanBodyShapeList.get(0).getUpperArm()); // 上臂围
            sportsman.setUpperArmTightening(sportsmanBodyShapeList.get(0).getUpperArmTightening()); // 上臂围紧
            sportsman.setBusts(sportsmanBodyShapeList.get(0).getBust()); // 胸围
            sportsman.setWaistlines(sportsmanBodyShapeList.get(0).getWaistline()); // 腰围
            sportsman.setHiplines(sportsmanBodyShapeList.get(0).getHipline()); // 臀围
            sportsman.setBodyFatPercentage(sportsmanBodyShapeList.get(0).getBodyFatPercentage()); // 体脂百分比
        }

        // 返回数据
        return AjaxResult.success(sportsman);
    }

    /**
     * 新增运动员
     */
    @PreAuthorize("@ss.hasPermi('training:sportsman:add')")
    @Log(title = "sportsman", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Sportsman sportsman) {

        // 判断此用户是否已经注册
        Sportsman sportsmanName = sportsmanService.getOne(Wrappers.<Sportsman>lambdaQuery().eq(Sportsman::getName , sportsman.getName()));
        if(StringUtils.isNotNull(sportsmanName)){
            System.out.println();
            return AjaxResult.error("此用户已经注册！" );
        }

        sportsman.setCtime(new Date());

        if(StringUtils.isNotEmpty(sportsman.getPassword())){
            sportsman.setPassword(SecurityUtils.encryptPassword(sportsman.getPassword()));  //用户密码
        }

        sportsmanService.save(sportsman);

        /**
         * 保存用户的身体形态
         */
        SportsmanBodyShape sportsmanBodyShape = new SportsmanBodyShape();
        sportsmanBodyShape.setSportsmanId(Long.parseLong(sportsman.getId().toString())); // 用户 id
        sportsmanBodyShape.setArmSpread(sportsman.getArmSpreads()); // 臂展
        sportsmanBodyShape.setKneelHigh(sportsman.getKneelHigh()); // 跪高（ 划艇 ）
        sportsmanBodyShape.setSitHigh(sportsman.getSitHighs()); // 坐高
        sportsmanBodyShape.setHeight(sportsman.getHeights()); // 身高
        sportsmanBodyShape.setShoulderWidth(sportsman.getShoulderWidths()); // 肩宽
        sportsmanBodyShape.setUpperArm(sportsman.getUpperArm()); // 上臂围
        sportsmanBodyShape.setUpperArmTightening(sportsman.getUpperArmTightening()); // 上臂围紧
        sportsmanBodyShape.setBust(sportsman.getBusts()); // 胸围
        sportsmanBodyShape.setWaistline(sportsman.getWaistlines()); // 腰围
        sportsmanBodyShape.setHipline(sportsman.getHiplines()); // 臀围
        sportsmanBodyShape.setBodyFatPercentage(sportsman.getBodyFatPercentage()); // 体脂百分比
        sportsmanBodyShape.setCtime(new Date()); // 创建时间
        sportsmanBodyShapeService.save(sportsmanBodyShape);
        return AjaxResult.success();
    }

    /**
     * 修改运动员
     */
    @PreAuthorize("@ss.hasPermi('training:sportsman:edit')")
    @Log(title = "sportsman", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Sportsman sportsman) {
        sportsman.setLtime(new Date());
        if(StringUtils.isNotEmpty(sportsman.getPassword())){
            sportsman.setPassword(SecurityUtils.encryptPassword(sportsman.getPassword()));  //用户密码
        }
        sportsmanService.updateById(sportsman);

        /**
         * 保存用户的身体形态
         */
        SportsmanBodyShape sportsmanBodyShape = new SportsmanBodyShape();
        sportsmanBodyShape.setSportsmanId(Long.parseLong(sportsman.getId().toString())); // 用户 id
        sportsmanBodyShape.setArmSpread(sportsman.getArmSpreads()); // 臂展
        sportsmanBodyShape.setKneelHigh(sportsman.getKneelHigh()); // 跪高（ 划艇 ）
        sportsmanBodyShape.setSitHigh(sportsman.getSitHighs()); // 坐高
        sportsmanBodyShape.setHeight(sportsman.getHeights()); // 身高
        sportsmanBodyShape.setShoulderWidth(sportsman.getShoulderWidths()); // 肩宽
        sportsmanBodyShape.setUpperArm(sportsman.getUpperArm()); // 上臂围
        sportsmanBodyShape.setUpperArmTightening(sportsman.getUpperArmTightening()); // 上臂围紧
        sportsmanBodyShape.setBust(sportsman.getBusts()); // 胸围
        sportsmanBodyShape.setWaistline(sportsman.getWaistlines()); // 腰围
        sportsmanBodyShape.setHipline(sportsman.getHiplines()); // 臀围
        sportsmanBodyShape.setBodyFatPercentage(sportsman.getBodyFatPercentage()); // 体脂百分比
        sportsmanBodyShape.setCtime(new Date()); // 创建时间
        sportsmanBodyShapeService.save(sportsmanBodyShape);

        return AjaxResult.success();
    }

    /**
     * 删除运动员
     */
    @PreAuthorize("@ss.hasPermi('training:sportsman:remove')")
    @Log(title = "sportsman", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        sportsmanService.removeByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }
    
}